package com.jp.knowledge.activity;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.jp.knowledge.R;
import com.jp.knowledge.adapter.FragMentAdapter;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.fragment.ImageFragment;
import com.jp.knowledge.logic.DownloadLogic;
import com.jp.knowledge.util.SignVerifyUtil;
import com.jp.knowledge.util.TimeUtil;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiayen     2017-06-08 15:37:53
 *
 * 图片浏览页面
 */
public class PhotoActivity extends SlidingActivity implements View.OnClickListener {

    @ViewInject(R.id.view_pageer)
    private ViewPager viewPager;

//
//    @ViewInject(R.id.top_layout)
//    private RelativeLayout  topLayout;
//
//    private PhotoAdapter adapter;

    private int postion;
    private List<String> list;

    private DownloadLogic  download;

    private FragMentAdapter  fragMentAdapter;

    private List<Fragment>  fragments;
    @Override
    protected int getResourceId() {
        Window window = getWindow();
        //隐藏标题栏
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隐藏状态栏
        //定义全屏参数
        int flag= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);

        return R.layout.activity_photo;
    }
    @Override
    protected void init() {

        download = new DownloadLogic(mContext,true);
        postion = getIntent().getIntExtra("position",0);
        list=getIntent().getStringArrayListExtra("imgs");

        if(list==null)return;
        fragments = new ArrayList<>();
        int size = list.size();
        for (int i = 0; i < size; i++) {
          fragments.add(new ImageFragment().newInstance(list.get(i)));
        }

        fragMentAdapter = new FragMentAdapter(getSupportFragmentManager(),fragments);




        topName.setText(postion+1+"/"+list.size());
        rightIcon.setVisibility(View.GONE);
        leftIcon.setImageResource(R.mipmap.guanzhufanhui);
        leftIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        rightBtn.setVisibility(View.VISIBLE);
        rightBtn.setText("保存");
        rightBtn.setOnClickListener(this);

//        adapter = new PhotoAdapter(mContext,list);
        viewPager.setOffscreenPageLimit(1);
        viewPager.setAdapter(fragMentAdapter);
        viewPager.setCurrentItem(postion);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                topName.setText(position+1+"/"+list.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.right_bn:
                String path =  mContext.getCacheDir().getPath()+"/img/"+ TimeUtil.formatYearData(System.currentTimeMillis())+"/"+ SignVerifyUtil.encrypt(list.get(viewPager.getCurrentItem()))+".jpg";
                download.download(list.get(viewPager.getCurrentItem()),path);
//                hideTop();
                break;
            default:
                break;
        }
    }
}
